package com.streamer.app.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.streamer.app.R
import com.streamer.app.bean.AnchorBean

class GiftRankAdapter(private val items: List<AnchorBean>) :
    RecyclerView.Adapter<GiftRankAdapter.ViewHolder>() {
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val ivEffectIcon: ImageView = view.findViewById(R.id.iv_effect_icon)
        val tvRank: TextView = view.findViewById(R.id.tv_rank)
        val ivAvatar: ImageView = view.findViewById(R.id.iv_avatar)
        val tvUserName: TextView = view.findViewById(R.id.tv_user_name)
        val ivCoin: ImageView = view.findViewById(R.id.iv_coin)
        val tvCoin: TextView = view.findViewById(R.id.tv_coin)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_gift_rank, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = items[position]
        // 排名前3显示特效icon，否则显示数字
//        if (item.rank in 1..3 && item.effectIconRes != 0) {
//            holder.ivEffectIcon.visibility = View.VISIBLE
//            holder.ivEffectIcon.setImageResource(item.effectIconRes)
//            holder.tvRank.visibility = View.GONE
//        } else {
//            holder.ivEffectIcon.visibility = View.GONE
//            holder.tvRank.visibility = View.VISIBLE
//            holder.tvRank.text = item.rank.toString()
//        }
        holder.ivAvatar.apply {
            Glide.with(this).load(item.image)
                .transform(RoundedCorners(50))
                .into(this)
        }
        holder.tvUserName.text = item.userName
        holder.ivCoin.setImageResource(R.mipmap.ic_coin)
        holder.tvCoin.text = item.points.toString()
    }

    override fun getItemCount(): Int = items.size
}